<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/10
 * Time: 10:50
 */

namespace Beerus\Modules\CategoryEntrance\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Illuminate\Http\Request;
use Biz\service\ShopCategoryEntrance as sShopCategoryEntrance;
use Illuminate\Pagination\LengthAwarePaginator;
use Biz\cache\CategoryEntrance as cCategoryEntrance;

class CategoryEntranceController extends Controller
{
    const BRAND_DEFAULT_LOGO = 'http://img.hhz1.cn/Op-imageShow/16a88305o05o00000owir9x';
    const PAGE_SIZE = 20;
    public  function  index(Request $request){

        $pageSize = self::PAGE_SIZE;
        $page = $request->input("page") ?: 1;
        $page = $page <1 ? 1 : $page;
        $limit = [($page - 1) * $pageSize, $pageSize];

        $result = sShopcategoryEntrance::admGetCategoryEntranceList($limit);

        $paginate = new LengthAwarePaginator($result['categoryEntranceList'], $result['categoryEntranceCount'], $pageSize, null, ['path' => url('admin/categoryentrance')]);

        $paginate->appends($request->all());

        $banner_pos = array(array('id'=>1,'pos'=>'商城首页'));


        $view = view('categoryEntrance::categoryentrance.index', [
                'banner_pos' => $banner_pos,
                'categoryentrance'     => $result['categoryEntranceList'],
                'page' => $paginate->render(),
            ]
        );

        return Admin::content(function (Content $content) use ($view) {
            $content->header('分类列表');
            $content->body($view);
        });
    }


    public function edit($id = 0)
    {
        $categoryEntranceInfo = [];
        if ($id) {

            $categoryEntranceInfo = sShopcategoryEntrance::admGetCategoryEntranceInfoById($id);
        }

        $banner_pos = array(array('id'=>1,'pos'=>'商城首页'));

        $view = view('categoryEntrance::categoryentrance.edit', [
                'banner_pos' => $banner_pos,
                'categoryentrance'     => $categoryEntranceInfo
            ]
        );


        return Admin::content(function (Content $content) use ($view) {
            $content->header('分类编辑');
            $content->body($view);
        });
    }

    public function save(Request $request)
    {
        $categoryEntranceidId = $request->input("categoryentranceid");
        $postData = $request->all();
        if ($categoryEntranceidId) {
            //编辑
            $categoryEntranceidId = sShopcategoryEntrance::admUpdateCategoryEntranceInfoById($categoryEntranceidId, $postData, Admin::user()->id);
        } else {
            //新建
            $categoryEntranceidId = sShopcategoryEntrance::admAddCategoryEntrance($postData, Admin::user()->id);
        }

        $categoryKey = sShopcategoryEntrance::admGetCategoryEntrancePage($categoryEntranceidId);
        $page      = ceil($categoryKey /self::PAGE_SIZE);
        ccategoryEntrance::delCategoryEntrance(sShopcategoryEntrance::CATEGORY_ENTRANCE_CACHHE_NAME);
        ccategoryEntrance::delCategoryEntrance(sShopcategoryEntrance::CATEGORY_ENTRANCE);

        if($categoryEntranceidId == 0){
            return response()->json([
                "code" => 1
            ]);
        }

        return response()->json([
            "code" => 0,
            "data" => [
                "categoryEntranceId" => $categoryEntranceidId,
                'page'      => $page,
            ]
        ]);
    }

    public function state(Request $request){

        $categoryEntranceIdId = $request->input("categoryentranceId");

        if ($categoryEntranceIdId) {

            $categoryEntranceIdId = sShopcategoryEntrance::amdStateCategoryEntranceById($categoryEntranceIdId);
        }
        ccategoryEntrance::delCategoryEntrance(sShopcategoryEntrance::CATEGORY_ENTRANCE_CACHHE_NAME);
        ccategoryEntrance::delCategoryEntrance(sShopcategoryEntrance::CATEGORY_ENTRANCE);

        if($categoryEntranceIdId == 0){
            return response()->json([
                "code" => 1
            ]);
        }

        return response()->json([
            "code" => 0,
            "data" => [
                "bannerId" => $categoryEntranceIdId
            ]
        ]);
    }

    public function offLine(Request $request){

        $categoryEntranceIdId = $request->input("categoryentranceId");

        if ($categoryEntranceIdId) {

            $categoryEntranceIdId = sShopcategoryEntrance::admOffLineCategoryEntranceById($categoryEntranceIdId);
        }
        ccategoryEntrance::delCategoryEntrance(sShopcategoryEntrance::CATEGORY_ENTRANCE_CACHHE_NAME);
        ccategoryEntrance::delCategoryEntrance(sShopcategoryEntrance::CATEGORY_ENTRANCE);

        if($categoryEntranceIdId == 0){
            return response()->json([
                "code" => 1
            ]);
        }

        return response()->json([
            "code" => 0,
            "data" => [
                "bannerId" => $categoryEntranceIdId
            ]
        ]);
    }
}